From d7e1ab2b9fcc57143e32fe84af110cfba01e2ca3 Mon Sep 17 00:00:00 2001 From: Daniel Sabo Date: Fri, 31 May 2013 13:25:28 -0700 Subject: [PATCH] Add some float conversions for grey --- extensions/Makefile.am | 2 + extensions/grey.c | 137 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 extensions/grey.c diff --git a/extensions/Makefile.am b/extensions/Makefile.am index 30ac8c5..ea5d89e 100644 --- a/extensions/Makefile.am +++ b/extensions/Makefile.am @@ -21,6 +21,7 @@ ext_LTLIBRARIES = \ gggl-lies.la \ gggl.la \ gimp-8bit.la \ + grey.la \ float.la \ fast-float.la \ naive-CMYK.la \ @@ -37,6 +38,7 @@ gegl_fixups_la_SOURCES = gegl-fixups.c gggl_lies_la_SOURCES = gggl-lies.c gggl_la_SOURCES = gggl.c gimp_8bit_la_SOURCES = gimp-8bit.c +grey_la_SOURCES = grey.c naive_CMYK_la_SOURCES = naive-CMYK.c HSV_la_SOURCES = HSV.c sse_fixups_la_SOURCES = sse-fixups.c diff --git a/extensions/grey.c b/extensions/grey.c new file mode 100644 index 0000000..a6fef28 --- /dev/null +++ b/extensions/grey.c @@ -0,0 +1,137 @@ +/* babl - dynamically extendable universal pixel conversion library. + * Copyright (C) 2013, Daniel Sabo + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * . + */ + +#include "config.h" +#include + +#include "babl.h" + +#include "base/util.h" +#include "base/rgb-constants.h" +#include "extensions/util.h" + +/* There was some debate on #gimp about whether these constants + * are accurate, for now I've elected to just follow whatever + * babl/base does. + * - Daniel + */ + +/* Float versions of the double constants in rgb-constants.h */ +static const float RGB_LUMINANCE_RED_FLOAT = RGB_LUMINANCE_RED; +static const float RGB_LUMINANCE_GREEN_FLOAT = RGB_LUMINANCE_GREEN; +static const float RGB_LUMINANCE_BLUE_FLOAT = RGB_LUMINANCE_BLUE; + +static long +conv_rgbaF_linear_y8_linear (unsigned char *src, + unsigned char *dst, + long samples) +{ + static const float RGB_LUMINANCE_RED_FLOAT = RGB_LUMINANCE_RED; + static const float RGB_LUMINANCE_GREEN_FLOAT = RGB_LUMINANCE_GREEN; + static const float RGB_LUMINANCE_BLUE_FLOAT = RGB_LUMINANCE_BLUE; + + float *s = (float *) src; + long n = samples; + + while (n--) + { + float value; + long int v; + value = *s++ * RGB_LUMINANCE_RED_FLOAT; + value += *s++ * RGB_LUMINANCE_GREEN_FLOAT; + value += *s++ * RGB_LUMINANCE_BLUE_FLOAT; + s++; + + v = rint (value * 255.0); + *dst++ = (v < 0) ? 0 : ((v > 255) ? 255 : v); + } + + return samples; +} + +static long +conv_rgbaF_linear_yF_linear (unsigned char *src, + unsigned char *dst, + long samples) +{ + + float *s = (float *) src; + float *d = (float *) dst; + long n = samples; + + while (n--) + { + float value; + value = *s++ * RGB_LUMINANCE_RED_FLOAT; + value += *s++ * RGB_LUMINANCE_GREEN_FLOAT; + value += *s++ * RGB_LUMINANCE_BLUE_FLOAT; + s++; + *d++ = value; + } + + return samples; +} + +static long +conv_rgbaF_linear_yaF_linear (unsigned char *src, + unsigned char *dst, + long samples) +{ + + float *s = (float *) src; + float *d = (float *) dst; + long n = samples; + + while (n--) + { + float value; + value = *s++ * RGB_LUMINANCE_RED_FLOAT; + value += *s++ * RGB_LUMINANCE_GREEN_FLOAT; + value += *s++ * RGB_LUMINANCE_BLUE_FLOAT; + *d++ = value; + *d++ = *s++; /* alpha */ + } + + return samples; +} + +int init (void); + +int +init (void) +{ + babl_conversion_new (babl_format ("RGBA float"), + babl_format ("Y u8"), + "linear", + conv_rgbaF_linear_y8_linear, + NULL); + + babl_conversion_new (babl_format ("RGBA float"), + babl_format ("Y float"), + "linear", + conv_rgbaF_linear_yF_linear, + NULL); + + babl_conversion_new (babl_format ("RGBA float"), + babl_format ("YA float"), + "linear", + conv_rgbaF_linear_yaF_linear, + NULL); + + return 0; +} -- 2.30.2